package cc.admin.modules.sys.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.sys.util.JwtUtil;
import cc.admin.common.util.oConvertUtils;
import cc.admin.modules.sys.entity.SysOrg;
import cc.admin.modules.sys.model.OrgIdModel;
import cc.admin.modules.sys.model.SysOrgTreeModel;
import cc.admin.modules.sys.service.ISysOrgService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 机构表 前端控制器
 *
 * @Author
 * @Since  2022-07-07
 */
@RestController
@RequestMapping("/sys/org")
@Slf4j
public class SysOrgController {

	@Autowired
	private ISysOrgService sysOrgService;

	/**
	 * 查询数据 查出所有机构,并以树结构数据格式响应给前端
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
	public Result<List<SysOrgTreeModel>> queryTreeList() {
		Result<List<SysOrgTreeModel>> result = new Result<>();
		try {
			List<SysOrgTreeModel> list = sysOrgService.queryTreeList();
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}


	/**
	 * 添加新数据 添加用户新建的机构对象数据,并保存到数据库
	 *
	 * @param sysOrg
	 * @return
	 */

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result<SysOrg> add(@RequestBody SysOrg sysOrg, HttpServletRequest request) {
		Result<SysOrg> result = new Result<SysOrg>();
		String username = JwtUtil.getUserNameByToken(request);
		try {
			sysOrg.setCreateBy(username);
			sysOrgService.saveOrgData(sysOrg, username);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}


	/**
	 * 编辑数据 编辑机构的部分数据,并保存到数据库
	 *
	 * @param sysOrg
	 * @return
	 */

	@RequestMapping(value = "/edit", method = RequestMethod.PUT)
	public Result<SysOrg> edit(@RequestBody SysOrg sysOrg, HttpServletRequest request) {
		String username = JwtUtil.getUserNameByToken(request);
		sysOrg.setUpdateBy(username);
		Result<SysOrg> result = new Result<SysOrg>();
		SysOrg sysOrgEntity = sysOrgService.getById(sysOrg.getId());
		if (sysOrgEntity == null) {
			result.error500("未找到对应实体");
		} else {
			boolean ok = sysOrgService.updateOrgDataById(sysOrg, username);
			if (ok) {
				result.success("修改成功!");
			}
		}
		return result;
	}


	/**
	 *   通过id删除
	 * @param id
	 * @return
	 */

	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public Result<SysOrg> delete(@RequestParam(name="id",required=true) String id) {

		Result<SysOrg> result = new Result<SysOrg>();
		SysOrg sysOrg = sysOrgService.getById(id);
		if(sysOrg==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = sysOrgService.delete(id);
			if(ok) {
				result.success("删除成功!");
			}
		}
		return result;
	}


	/**
	 * 批量删除 根据前端请求的多个ID,对数据库执行删除相关机构数据的操作
	 *
	 * @param ids
	 * @return
	 */

	@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
	public Result<SysOrg> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {

		Result<SysOrg> result = new Result<SysOrg>();
		if (ids == null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		} else {
			this.sysOrgService.deleteBatchWithChildren(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}


	/**
	 * 查询数据 添加或编辑页面对该方法发起请求,以树结构形式加载所有机构的名称,方便用户的操作
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryIdTree", method = RequestMethod.GET)
	public Result<List<OrgIdModel>> queryIdTree() {
		Result<List<OrgIdModel>> result = new Result<>();
		try {
			List<OrgIdModel> list = sysOrgService.queryOrgIdTreeList();
			result.setResult(list);
			result.setSuccess(true);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return result;
	}

	/**
	 * 查询所有机构信息
	 * @return
	 */
	@GetMapping("listAll")
	public Result<List<SysOrg>> listAll(@RequestParam(name = "id", required = false) String id) {
		Result<List<SysOrg>> result = new Result<>();
		LambdaQueryWrapper<SysOrg> query = new LambdaQueryWrapper<SysOrg>();
		query.orderByAsc(SysOrg::getCode);
		if(oConvertUtils.isNotEmpty(id)){
			String arr[] = id.split(",");
			query.in(SysOrg::getId,arr);
		}
		List<SysOrg> ls = this.sysOrgService.list(query);
		result.setSuccess(true);
		result.setResult(ls);
		return result;
	}

	/**
	 * 获取机构
	 * @return
	 */
	@RequestMapping(value = "/getMyAll", method = RequestMethod.GET)
	public List<Map<String,String>> getMyAll(){
		return sysOrgService.getMyAll();
	}

	@RequestMapping(value = "/getOrgCodeName", method = RequestMethod.GET)
	public List<Map<String,String>> getOrgCodeName(){

		return sysOrgService.getOrgCodeName();
	}
}
